package leetcode.code543.DiameterofBinaryTree;

import leetcode.helper.tree.TreeNode;

public class Solution543 extends DiameterofBinaryTree {

	@Override
	public int diameterOfBinaryTree(TreeNode root) {
		this.tree(root);
		return root.val;
	}

	/**
	 * root点左右中最长的边数 val 该点左、右、经过root的最大值
	 *
	 * @param root
	 * @return
	 */
	public int tree(TreeNode root) {
		int l = 0;
		int r = 0;
		int lv = 0;
		int rv = 0;
		if (root.left != null) {
			l = tree(root.left) + 1;
			lv = root.left.val;
		}
		if (root.right != null) {
			r = tree(root.right) + 1;
			rv = root.right.val;
		}
		root.val = Math.max(Math.max(lv, rv), l + r);
		return Math.max(l, r);
	}

	public static void main(String[] args) {
		Solution543 so = new Solution543();
//		so.debug1();
//		so.debug2();
		so.debug3();
	}

}
